package com.marketlive.app.b2c.struts;

import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import org.apache.struts.util.ModuleException;
//import org.marketlive.biz.session.context.ICommerceSession;

public class AggregateExceptionHandler extends ExceptionHandler
{
  private static Log log = LogFactory.getLog(AggregateExceptionHandler.class);
  private static final String defaultPage = "/index.jsp";
  private final String ERROR_PAGE_404 = "/jump.do?itemType=Error404";

  private final String CATEGORY_RESOLUTION_ERROR = "resolve category";
  private final String PRODUCT_RESOLUTION_ERROR = "resolve product";
  private final String UNABLE_TO_RESOLVE_ERROR = "Unable to resolve";
  private final String ID_RESOLUTION_ERROR = "No row with the given identifier exists";
  private final String ENTITY_DELETED_ERROR = "has been deleted";
  private final String ENTITY_INACTIVE_ERROR = "is not active";

  public ActionForward execute(Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response)
    throws ServletException
  {
    ActionErrors storedErrors = null;

    ActionForward forward = super.execute(ex, ae, mapping, formInstance, request, response);

    log.error("Caught exception", ex);

    // ICommerceSession
    Object session = request.getSession().getAttribute("COMMERCE_SESSION");

    if (session == null) {
      forward = new ActionForward("/index.jsp", true);
    }

    String errMessage = "";
    String errCause = "";

    if (ex.getMessage() != null) {
      errMessage = ex.getMessage();
    }

    if ((ex.getCause() != null) && (ex.getCause().getMessage() != null)) {
      errCause = ex.getCause().getMessage();
    }

    if ((errMessage.startsWith("No row with the given identifier exists")) || (errCause.startsWith("No row with the given identifier exists")) || (errMessage.indexOf("resolve category") != -1) || (errCause.indexOf("resolve category") != -1) || (errMessage.indexOf("resolve product") != -1) || (errCause.indexOf("resolve product") != -1) || (errMessage.indexOf("Unable to resolve") != -1) || (errCause.indexOf("Unable to resolve") != -1) || (errMessage.indexOf("has been deleted") != -1) || (errCause.indexOf("has been deleted") != -1) || (errMessage.indexOf("is not active") != -1) || (errCause.indexOf("is not active") != -1))
    {
      forward = new ActionForward("/jump.do?itemType=Error404", false);
    }

    AggregateException age = new AggregateException();
    age.add(ex);

    storedErrors = (ActionErrors)request.getSession().getAttribute("org.apache.struts.action.ERROR");

    List exceptionList = age.getExceptions();
    Iterator errors = exceptionList.iterator();

    while (errors.hasNext()) {
      Exception e = (Exception)errors.next();
      ExceptionConfig config = mapping.findException(e.getClass());

      ActionMessage error = null;
      String property = null;

      if ((e instanceof ModuleException)) {
        error = ((ModuleException)e).getActionMessage();
        property = ((ModuleException)e).getProperty();
      } else {
        error = new ActionMessage(config.getKey(), e.getMessage());
        property = error.getKey();
      }

      request.setAttribute("javax.servlet.error.exception", e);

      if (storedErrors == null) {
        storedErrors = new ActionErrors();
      }
      storedErrors.add(property, error);
    }

    return forward;
  }
}